//app.js
App({
  onLaunch: function() {
    //隐藏系统tabbar
    wx.hideTabBar();
    this.getSystemInfo();
    if (this.globalData.accessToken === '') {
      this.getAccessToken(); //获取 AccessToken
    };
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo
              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
  },
  //刷新accessToken 用code换取accessToken
  getAccessToken: function() {
    var that = this;
    wx.login({
      success: function(res) {
        if (res.code) {
          wx.request({
            url: that.globalData.hostUrl + 'login',
            data: {
              code: res.code,
            },
            success: function(res) {

              res = res.data;
              //设置全局变量 accessToken
              that.globalData.accessToken = 'Bearer' + ' ' + res.data.token;
              //设置缓存变量 accessToken
              wx.setStorageSync('accessToken', 'Bearer' + ' ' + res.data.token);
              var num = wx.getStorageSync('accessToken');
              // var dd = that.globalData.accessToken;

            }
          })
        }
      }
    })
  },
  //供wGet调用的http请求
  requestGet: function(url, params) {
    var that = this;
    return new Promise(function(resolve, reject) {
      wx.request({
        url: that.globalData.hostUrl + url,
        data: params,
        method: 'GET',
        header: {
          'content-type': 'application/json',
          'Accept': 'application/json',
          'Authorization': wx.getStorageSync('accessToken')
        },
        success: function(resDate) {
          if (resDate.statusCode === 200) {
            resolve(resDate);
          } else {
            reject(false);
          }
        },
      });
    });
  },
  //供wPost调用的http请求
  requestPost: function(url, params) {
    var that = this;
    return new Promise(function(resolve, reject) {
      wx.request({
        url: that.globalData.hostUrl + url,
        data: params,
        method: 'POST',
        header: {
          'content-type': 'application/json',
          'Accept': 'application/json',
          'Authorization': wx.getStorageSync('accessToken')
        },
        success: function(resDate) {
          if (resDate.statusCode === 200) {
            resolve(resDate);
          } else {
            reject(false);
          }
        },
      });
    });
  },
  //刷新accessToken 用code换取accessToken
  updateAccessToken: function() {
    var that = this;
    return new Promise(function(resolve, reject) {
      wx.login({
        success: function(res) {
          if (res.code) {
            wx.request({
              url: that.globalData.hostUrl + 'login',
              data: {
                code: res.code,
              },
              success: function(res) {

                res = res.data;
                //设置全局变量 accessToken
                that.globalData.accessToken = 'Bearer' + ' ' + res.data.token;
                //设置缓存变量 accessToken
                wx.setStorageSync('accessToken', 'Bearer' + ' ' + res.data.token);
                if (res) {
                  resolve('Bearer' + ' ' + res.data.token);
                } else {
                  reject(false);
                }

              }
            })
          }
        }
      })
    });
  },
  getSystemInfo: function() {
    let t = this;
    wx.getSystemInfo({
      success: function(res) {
        t.globalData.systemInfo = res;
      }
    });
  },
  editTabbar: function () {
    let tabbar = this.globalData.tabBar;
    let currentPages = getCurrentPages();
    let _this = currentPages[currentPages.length - 1];
    let pagePath = _this.route;
    (pagePath.indexOf('/') != 0) && (pagePath = '/' + pagePath);
    for (let i in tabbar.list) {
      tabbar.list[i].selected = false;
      (tabbar.list[i].pagePath == pagePath) && (tabbar.list[i].selected = true);
    }
    _this.setData({
      tabbar: tabbar
    });
  },
  globalData: {
    buttonClicked:false,//防止按钮重复点击
    userInfo: null,
    testUrl0: '',
    testUrl1: '',
    testUrl2: '',
    code: '',
    name: '',
    userId: '',
    add: {
      "id": ''
    },
    getUserInfo: '',
    hostUrl: "https://wechat.wuyuzhong.com/api/",
    accessToken: wx.getStorageSync('accessToken'),
    tabBar: {
      'tabbarNum': 1,
      "backgroundColor": "#ffffff",
      "color": "#979795",
      "selectedColor": "#1c1c1b",
      "list": [{
          "pagePath": "/pages/index/index",
          "iconPath": "icon/gif.png",
          "selectedIconPath": "icon/gif_active.png",
          "text": "首页"
        },
        {
          "pagePath": "/pages/add/add",
          "iconPath": "icon/add.png",
          "isSpecial": true,
          "text": "发起抽奖"
        },
        {
          "pagePath": "/pages/user/user",
          "iconPath": "icon/user.png",
          "selectedIconPath": "icon/user_active.png",
          "text": "我的"
        }
      ]
    }
  }
})